<style lang="less">
@import './rulesManage.less';
</style>
<template>
  <div class="search">
    <Row>
      <Col>
      <Card>

        <Row type='flex'>
          <Col order=1>
          <span>请选择公司</span>
          <Select v-model="companyId"
                  @on-change='onCompanySelectChange'
                  placeholder="请选择公司"
                  style="width:auto;">
            <Option v-for="item in companyList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
          </Select>
          </Col>
          <Col order=2
               span="1"
               style="text-align: center">
          </Col>
          <Col order=3>

          <Form ref="searchForm"
                :model="searchForm"
                inline
                :label-width="70"
                @submit.native.prevent
                class="search-form">
            <Form-item label="规则名称"
                       prop="ruleName">
              <Input type="text"
                     v-model="searchForm.ruleName"
                     clearable
                     placeholder="请输入规则名"
                     style="width: 200px" />
            </Form-item>
            <Form-item style="margin-left:-35px;"
                       class="br">
              <Button @click="handleSearch"
                      type="primary"
                      icon="ios-search">
                搜索
              </Button>
              <Button @click="handleReset">重置</Button>
            </Form-item>
          </Form>
          </Col>
        </Row>

        <Row type="flex">
          <Col>
          <RadioGroup v-model="ruleType"
                      type="button"
                      @on-change='onRuleTypeSelectChange'
                      style="height: 58px;">
            <Radio border
                   label=1>超速规则</Radio>
            <Radio border
                   label=2>疲劳规则</Radio>
            <Radio border
                   label=3>区域规则</Radio>
            <Radio border
                   label=4>关键点规则</Radio>
            <!--<Radio border label=5>路线路段规则</Radio>-->
            <Radio border
                   label=6>车辆异常规则</Radio>
          </RadioGroup>
          </Col>
        </Row>

        <Row class="operation">
          <Button @click="add"
                  v-has="'add'"
                  type="primary"
                  icon="md-add">添加规则
          </Button>
        </Row>
        <Row type="flex">

          <Table :loading="loading"
                 border
                 :columns="columns"
                 :data="data"
                 sortable="custom"
                 @on-sort-change="changeSort"
                 @on-selection-change="showSelect"
                 ref="table"></Table>
          <Table :columns="exportColumns"
                 :data="exportData"
                 ref="exportTable"
                 style="display:none"></Table>

        </Row>
        <Row type="flex"
             justify="end"
             class="page">
          <Page :current="searchForm.current"
                :total="total"
                :page-size="searchForm.size"
                @on-change="changePage"
                @on-page-size-change="changePageSize"
                :page-size-opts="[10,20,50,100]"
                size="small"
                show-total
                show-elevator
                show-sizer></Page>
        </Row>
      </Card>
      </Col>
    </Row>
    <Modal :title="modalTitle"
           v-model="ruleModalVisible"
           :mask-closable="false"
           :width="700"
           :styles="{top: '30px'}">
      <Form ref="ruleForm"
            :model="ruleForm"
            :label-width="140"
            label-position='right'
            :rules="ruleFormValidate">

        <FormItem label="规则名称"
                  prop='ruleName'>
          <Input v-model="ruleForm.ruleName"
                 placeholder='请输入规则名称'></Input>
        </FormItem>
        <FormItem v-if="true"
                  label="播报内容"
                  prop='alarmPlayText'>
          <Input v-model="ruleForm.alarmPlayText"
                 placeholder='请输入报警时播报的内容'></Input>
        </FormItem>
        <FormItem v-if="ruleType == 1 || ruleType == 3"
                  label="区域类型">
          <RadioGroup v-model="ruleForm.areaType"
                      @on-change="onAreaTypeChange">
            <Radio v-for="item in areaTypeList"
                   :label="item.value"
                   :value="item.value"
                   :key="item.value">{{item.label}}
            </Radio>
          </RadioGroup>
        </FormItem>
        <FormItem v-if="ruleForm.areaType == 1 || ruleForm.areaType == 2"
                  :label="ruleForm.areaType == 1 ? '区域名称' : '线路选择'"
                  prop='customAreaId'>
          <Select v-model="ruleForm.customAreaId"
                  placeholder="请选择"
                  style="width: 350px">
            <Option v-for="item in areaLineList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
          </Select>
        </FormItem>
        <FormItem v-if="ruleType == 1 || ruleType == 2"
                  label="时速限速"
                  prop='speedLimit'>
          <Select v-model="ruleForm.speedLimit"
                  style="width: 350px">
            <Option v-for="item in speedLimitList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
          </Select>
          <span>公里/时</span>
        </FormItem>

        <FormItem v-if="ruleType == 2"
                  label="疲劳持续时长"
                  prop='tiredTime'>
          <Select v-model="ruleForm.tiredTime"
                  style="width: 350px">
            <Option v-for="item in tiredTimeList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
          </Select>
          <span>分钟</span>
        </FormItem>

        <FormItem v-if="ruleType == 2"
                  label="休息时长"
                  prop='restTime'>
          <Select v-model="ruleForm.restTime"
                  style="width: 350px">
            <Option v-for="item in restTimeList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
          </Select>
          <span>分钟</span>
        </FormItem>

        <FormItem v-if="ruleType == 1"
                  label="时长限制"
                  prop='timeLimit'>
          <Select v-model="ruleForm.timeLimit"
                  style="width: 350px">
            <Option v-for="item in keepTimeList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
          </Select>
          <span>秒</span>
        </FormItem>
        <FormItem v-if="true"
                  label="每天开始时间"
                  prop='limitStart'>
          <TimePicker type="time"
                      placeholder="开始时间"
                      v-model="ruleForm.limitStart"></TimePicker>
        </FormItem>
        <FormItem v-if="true"
                  label="每天结束时间"
                  prop='limitEnd'>
          <TimePicker type="time"
                      placeholder="结束时间"
                      v-model="ruleForm.limitEnd"></TimePicker>
        </FormItem>

        <FormItem v-if='ruleType == 3'
                  label="规则类型"
                  prop="areaRuleType">
          <RadioGroup v-model="ruleForm.areaRuleType"
                      @on-change="onAreaRuleTypeChange">
            <Radio :label="true">进入区域报警</Radio>
            <Radio :label="false">离开区域报警</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='false'
                  label="进入区域报警"
                  prop="areaInAlarm">
          <RadioGroup v-model="ruleForm.areaInAlarm">
            <Radio :label="true">是</Radio>
            <Radio :label="false">否</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='false'
                  label="离开区域报警"
                  prop="areaOutAlarm">
          <RadioGroup v-model="ruleForm.areaOutAlarm">
            <Radio :label="true">是</Radio>
            <Radio :label="false">否</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='false'
                  label="关键点未到达报警"
                  prop="pointUnarrive">
          <RadioGroup v-model="ruleForm.pointUnarrive">
            <Radio :label="true">是</Radio>
            <Radio :label="false">否</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='false'
                  label="关键点未离开报警"
                  prop="pointUnleave">
          <RadioGroup v-model="ruleForm.pointUnleave">
            <Radio :label="true">是</Radio>
            <Radio :label="false">否</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='ruleType == 4'
                  label="关键点类型"
                  prop="pointRuleType">
          <RadioGroup v-model="ruleForm.pointRuleType"
                      @on-change="onPointRuleTypeChange">
            <Radio :label="true">关键点未到达报警</Radio>
            <Radio :label="false">关键点未离开报警</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='false'
                  label="路线行驶过长"
                  prop='lineFaraway'>
          <Input v-model="ruleForm.lineFaraway"
                 placeholder='请输入'></Input>
        </FormItem>

        <FormItem v-if='false'
                  label="路线行驶不足"
                  prop='lineShortage'>
          <Input v-model="ruleForm.lineShortage"
                 placeholder='请输入'></Input>
        </FormItem>

        <FormItem v-if='false'
                  label="路线限速"
                  prop='lineSpeedLimit'>
          <Input v-model="ruleForm.lineSpeedLimit"
                 placeholder='请输入'></Input>
        </FormItem>

        <FormItem v-if='false'
                  label="异常离线报警"
                  prop="offlineUnnormal">
          <RadioGroup v-model="ruleForm.offlineUnnormal">
            <Radio :label="true">是</Radio>
            <Radio :label="false">否</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='false'
                  label="离线位移报警"
                  prop="offlineShift">
          <RadioGroup v-model="ruleForm.offlineShift">
            <Radio :label="true">是</Radio>
            <Radio :label="false">否</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='ruleType == 6'
                  label="车辆异常规则类型"
                  prop="offlineRuleType">
          <RadioGroup v-model="ruleForm.offlineRuleType"
                      @on-change="onOfflineRuleTypeChange">
            <Radio :label="true">异常离线报警</Radio>
            <Radio :label="false">离线位移报警</Radio>
          </RadioGroup>
        </FormItem>

        <FormItem v-if='ruleType == 6'
                  label="最后gps车速"
                  prop='lastGpsSpeed'>
          <Select v-model="ruleForm.lastGpsSpeed"
                  style="width: 350px">
            <Option v-for="item in lastGPSSpeedList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
            <span>KM/H</span>
          </Select>
        </FormItem>

        <FormItem v-if='false'
                  label="最后gps离线时长"
                  prop='offlineTime'>
          <Input v-model="ruleForm.offlineTime"
                 placeholder='请输入离线时长 单位: 分钟'
                 style="width: 350px"></Input>
          <span>分钟</span>
        </FormItem>

        <FormItem v-if='false'
                  label="最后gps离线时长"
                  prop='offlineShiftDistance'>
          <Input v-model="ruleForm.offlineShiftDistance"
                 placeholder='请输入离线再上线后位置超过 单位: 米'
                 style="width: 350px"></Input>
          <span>米</span>
        </FormItem>

        <FormItem v-if="true"
                  label="风险级别">
          <Select v-model="ruleForm.riskLevel">
            <Option v-for="item in riskLevelList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
          </Select>
        </FormItem>
        <FormItem v-if="true"
                  label="扣分系数">
          <Select v-model="ruleForm.score">
            <Option v-for="item in scoreList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}
            </Option>
          </Select>
        </FormItem>

      </Form>

      <div slot="footer">
        <Button type="text"
                @click="ruleModalVisible = false">取消</Button>
        <Button type="primary"
                :loading="submitLoading"
                @click="submitRule">提交
        </Button>
      </div>
    </Modal>
    <Modal v-model="modalExportAll"
           title="确认导出"
           :loading="loadingExport"
           @on-ok="exportAll">
      <p>您确认要导出全部 {{total}} 条数据？</p>
    </Modal>

    <check-password ref="checkPass"
                    @on-success="resetPass" />
  </div>
</template>

<script>
import {
  getRuleList,
  addRule,
  deleteRule,
  updateRule,
  getCompanyCombo
} from '@/api/index'
import { getCustomLine, getCustomArea } from '@/api/operate'
import { validateMobile, validatePassword } from '@/libs/validate'
import { Message } from 'view-design'

import {
  ruleTypeList,
  areaTypeList,
  areaTypeList2,
  riskLevelList,
  scoreList,
  speedLimitList,
  speedLimitList2,
  keepTimeList,
  tiredTimeList,
  restTimeList,
  lastGPSSpeedList
} from '@/libs/dict.js'

import departmentChoose from '../../my-components/xboot/department-choose'
import departmentTreeChoose from '../../my-components/xboot/department-tree-choose'
import uploadPicInput from '../../my-components/xboot/upload-pic-input'
import circleLoading from '../../my-components/circle-loading.vue'
import checkPassword from '@/views/my-components/xboot/check-password'
import messageTip from '../../main-components/message-tip.vue'
import util from '@/libs/util'

export default {
  name: 'user-manage',
  components: {
    circleLoading,
    departmentChoose,
    departmentTreeChoose,
    uploadPicInput,
    checkPassword
  },
  data() {
    return {
      ruleTypeList: ruleTypeList,
      areaTypeList: areaTypeList,
      riskLevelList: riskLevelList,
      scoreList: scoreList,
      speedLimitList: speedLimitList,
      keepTimeList: keepTimeList,
      areaLineList: [], // 区域和线路都用这个接收数据
      tiredTimeList: tiredTimeList,
      restTimeList: restTimeList,
      lastGPSSpeedList: lastGPSSpeedList,
      ruleType: '1',
      companyId: 0,
      companyList: [],
      switchVal: true,
      loading: true,
      operationLoading: false,
      loadingExport: true,
      modalExportAll: false,
      drop: false,
      dropDownContent: '展开',
      dropDownIcon: 'ios-arrow-down',
      selectCount: 0,
      selectList: [],
      dataDep: [],
      deptCasc: [], //部门级联data
      searchKey: '',
      searchForm: {
        ruleName: '',
        current: 1,
        size: 100,
        sort: 'createTime',
        order: 'desc',
        startDate: '',
        endDate: ''
      },
      selectDate: null,
      modalType: 0,
      ruleModalVisible: false,
      modalTitle: '',
      speedFasterRuleForm: {
        ruleName: '',
        popup: true,
        areaType: 1
      },
      ruleForm: {},
      cloneForm: {
        ruleName: '规则00', //规则名称
        companyId: 0,
        ruleId: 0, // 规则ID
        ruleType: 1, //1=超速，2=疲劳，3=区域，4=超时停车，5=道路等级限速，6=关键点规则，7=路线路段规则，8=限行规则，9=车辆异常规则
        popup: true, // 是否启用弹窗
        riskLevel: 0, // 风险等级 1-5
        score: 0, // 分数
        alarmPlayText: '有报警', // 报警播放内容
        areaType: 0, // 0=不限定 1=自定义区域 2=线路
        customAreaId: 0, // 自定义区域id
        restTime: 1, // 休息时长 单位:分钟
        speedLimit: 30, // 时速限制 公里/时
        timeLimit: 60, // 时长限制 秒
        limitStart: '', // 每日时间范围开始
        limitEnd: '', // 每日时间范围结束
        tiredTime: 1, // 疲劳持续时长，分钟
        areaInAlarm: true, //进入区域报警
        areaOutAlarm: true, // 离开区域报警
        areaRuleType: true, // 本地标识服务端不读取此字段, true=进入区域; false=离开区域;
        pointUnarrive: true, // 关键点未到达
        pointUnleave: true, // 关键点未离开
        pointRuleType: true, // 本地标识服务端不读取此字段, true=关键点未到达; false=关键点未离开;
        lineIn: true, // 进入线路报警
        lineOut: true, // 离线线路报警
        lineFaraway: '1', // 路线行驶过长（拐点经纬度|宽度|阈值）
        lineShortage: '1', // 路线行驶不足（拐点经纬度|宽度|阈值）
        lineSpeedLimit: '1', // 路线限速（拐点经纬度|宽度|时速|时长）
        offlineUnnormal: true, // 异常离线
        offlineShift: true, // 离线位移
        offlineRuleType: true, // 本地标识服务端不读取此字段, true=异常离线; false=离线位移;
        lastGpsSpeed: 1, // 最后gps车速
        offlineTime: 1, // 离线时长 分钟
        offlineShiftDistance: 1, // 离线再上线后位置超过(米)
        createBy: '', // 创建者
        createDate: '' // 创建时间
      },
      errorPass: '',
      ruleFormValidate: {
        ruleName: [
          {
            required: true,
            message: '规则名不能为空',
            trigger: 'blur'
          }
        ]
      },
      submitLoading: false,
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 65,
          align: 'center',
          fixed: 'left'
        },
        {
          title: '规则名称',
          key: 'ruleName',
          minWidth: 120,
          align: 'center',
          sortable: true
        },
        {
          title: '扣分系数',
          key: 'score',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h(
              'span',
              this.scoreList.find(item => {
                return item.value == params.row.score
              }).label
            )
          }
        },
        {
          title: '风险等级',
          key: 'riskLevel',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h(
              'span',
              this.riskLevelList.find(item => {
                return item.value == params.row.riskLevel
              }).label
            )
          }
        },
        {
          title: '最近操作时间',
          key: 'createDate',
          sortable: true,
          align: 'center',
          sortType: 'desc',
          minWidth: 180
        },
        {
          title: '操作人',
          key: 'createBy',
          minWidth: 90,
          align: 'center'
        },
        {
          title: '数据操作',
          key: 'action',
          width: 140,
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  directives: [
                    {
                      name: 'has',
                      value: 'edit'
                    }
                  ],
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.edit(params.row)
                    }
                  }
                },
                '编辑'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  directives: [
                    {
                      name: 'has',
                      value: 'delete'
                    }
                  ],
                  on: {
                    click: () => {
                      this.remove(params.row)
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      exportColumns: [
        {
          title: '规则名',
          key: 'username'
        },
        {
          title: '头像',
          key: 'avatar'
        },
        {
          title: '所属部门ID',
          key: 'departmentId'
        },
        {
          title: '所属部门',
          key: 'departmentTitle'
        },
        {
          title: '手机',
          key: 'mobile'
        },
        {
          title: '邮箱',
          key: 'email'
        },
        {
          title: '规则类型',
          key: 'type'
        },
        {
          title: '状态',
          key: 'status'
        },
        {
          title: '删除标志',
          key: 'delFlag'
        },
        {
          title: '创建时间',
          key: 'createTime'
        },
        {
          title: '更新时间',
          key: 'updateTime'
        }
      ],
      data: [],
      exportData: [],
      total: 0
    }
  },
  watch: {
    ruleType(newValue, oldValue) {
      if (newValue == 1) {
        this.speedLimitList = speedLimitList
      } else if (newValue == 2) {
        this.speedLimitList = speedLimitList2
      } else if (newValue == 3) {
        this.ruleForm.areaType = areaTypeList2[0].value
        this.areaTypeList = areaTypeList2
      }
    }
  },
  computed: {
    showRuleFields: function() {
      let visible = true
      if (this.ruleType) {
        visible = true
      }
    }
  },
  methods: {
    init() {
      // this.requestRuleList();
      this.requestCompanyList()
    },
    handleSelectDepTree(v) {
      this.ruleForm.deptId = v[0]
    },
    handleSelectDep(v) {
      this.searchForm.deptId = v
    },
    changePage(v) {
      this.searchForm.current = v
      this.requestRuleList()
      this.clearSelectAll()
    },
    changePageSize(v) {
      this.searchForm.size = v
      this.requestRuleList()
    },
    selectDateRange(v) {
      if (v) {
        this.searchForm.startDate = v[0]
        this.searchForm.endDate = v[1]
      }
    },
    /*
     * 获取企业下拉框数据
     */
    requestCompanyList() {
      this.loading = true
      getCompanyCombo().then(res => {
        console.log('getCompanyCombo', res)
        this.loading = false
        if (res.code == 0) {
          this.companyList = res.data
          this.companyId = util.min(this.companyList).value
          this.requestRuleList()
        }
      })
    },
    /*
     * 获取规则列表
     */
    requestRuleList() {
      if (this.companyId) {
        this.loading = true
        let params = util.deepClone(this.searchForm)
        params.ruleType = this.ruleType
        params.companyId = this.companyId
        getRuleList(params).then(res => {
          console.log('requestRuleList', res)
          this.loading = false
          if (res.code == 0) {
            this.data = res.data.records
            this.total = res.data.total
          }
        })
      } else {
        Message.error('请选择公司')
      }
    },

    /**
     * 请求自定义区域数据
     */
    requestCustomArea() {
      getCustomArea({ companyId: this.companyId }).then(res => {
        console.log('getCustomArea', res)
        if (res.code == 0) {
          this.areaLineList = res.data
        }
      })
    },

    /**
     * 请求自定义线路数据
     */
    requestCustomLine() {
      getCustomLine({ companyId: this.companyId }).then(res => {
        console.log('getCustomLine', res)
        if (res.code == 0) {
          this.areaLineList = res.data
        }
      })
    },
    handleSearch() {
      this.searchForm.current = 1
      this.searchForm.size = 100
      this.requestRuleList()
    },
    handleReset() {
      this.ruleForm = util.deepClone(this.cloneForm)
      this.searchForm.current = 1
      this.searchForm.size = 100
      this.searchForm.ruleName = ''
      // 重新加载数据
      this.requestRuleList()
    },
    changeSort(e) {
      this.searchForm.sort = e.key
      this.searchForm.order = e.order
      if (e.order == 'normal') {
        this.searchForm.order = ''
      }
      this.requestRuleList()
    },
    exportAll() {
      getAllUserData().then(res => {
        this.modalExportAll = false
        if (res.success) {
          this.exportData = res.result
          setTimeout(() => {
            this.$refs.exportTable.exportCsv({
              filename: '规则全部数据'
            })
          }, 1000)
        }
      })
    },
    submitRule() {
      console.log('submitRule')
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          console.log('ruleForm 验证通过')
          let params = JSON.parse(JSON.stringify(this.ruleForm))
          params.companyId = Number(this.companyId)
          params.ruleType = Number(this.ruleType)
          if (this.modalType == 0) {
            // 添加规则 避免编辑后传入id
            this.submitLoading = true
            addRule(params).then(res => {
              this.submitLoading = false
              if (res.code == 0) {
                this.$Message.success('操作成功')
                this.requestRuleList()
                this.ruleModalVisible = false
              } else {
                this.$Message.error(res.msg)
              }
            })
          } else {
            // 编辑
            this.submitLoading = true

            updateRule(params).then(res => {
              this.submitLoading = false
              if (res.code == 0) {
                this.$Message.success('操作成功')
                this.requestRuleList()
                this.ruleModalVisible = false
              } else {
                this.$Message.error(res.msg)
              }
            })
          }
        } else {
          console.log('ruleForm 验证未通过')
        }
      })
    },
    add() {
      this.modalType = 0
      this.modalTitle = '添加规则'
      this.ruleForm = util.deepClone(this.cloneForm)
      this.ruleModalVisible = true
    },
    edit(v) {
      this.modalType = 1
      this.modalTitle = '编辑规则'
      this.ruleForm = util.deepClone(this.cloneForm)
      this.ruleForm = JSON.parse(JSON.stringify(v))
      // this.$refs.depTree.setData(
      //   this.ruleForm.deptName
      // );

      if (this.ruleForm.status == 1) this.switchVal = true
      else this.switchVal = false
      this.ruleModalVisible = true
    },
    remove(v) {
      this.$Modal.confirm({
        title: '确认删除',
        content: '您确认要删除 ' + v.ruleName + ' ?',
        loading: true,
        onOk: () => {
          deleteRule(v.ruleId).then(res => {
            this.$Modal.remove()
            if (res.code == 0) {
              this.$Message.success('删除成功')
              this.requestRuleList()
            } else {
              this.$Message.error(res.msg)
            }
          })
        }
      })
    },
    dropDown() {
      if (this.drop) {
        this.dropDownContent = '展开'
        this.dropDownIcon = 'ios-arrow-down'
      } else {
        this.dropDownContent = '收起'
        this.dropDownIcon = 'ios-arrow-up'
      }
      this.drop = !this.drop
    },
    showSelect(e) {
      this.exportData = e
      this.selectList = e
      this.selectCount = e.length
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false)
    },
    delAll() {
      if (this.selectCount <= 0) {
        this.$Message.warning('您还未选择要删除的数据')
        return
      }
      this.$Modal.confirm({
        title: '确认删除',
        content: '您确认要删除所选的 ' + this.selectCount + ' 条数据?',
        loading: true,
        onOk: () => {
          let ids = ''
          this.selectList.forEach(function(e) {
            ids += e.id + ','
          })
          ids = ids.substring(0, ids.length - 1)
          deleteRule(ids).then(res => {
            this.$Modal.remove()
            if (res.success) {
              this.$Message.success('删除成功')
              this.clearSelectAll()
              this.requestRuleList()
            }
          })
        }
      })
    },
    onRuleTypeSelectChange(e) {
      console.log('onRuleTypeSelectChange', e)
      this.ruleType = e
      this.requestRuleList()
    },
    onCompanySelectChange(e) {
      console.log('onCompanySelectChange', e)
      this.companyId = e
      this.requestRuleList()
    },
    onAreaRuleTypeChange(e) {
      console.log('onAreaRuleTypeChange', e)
      if (e) {
        this.ruleForm.areaInAlarm = true
        this.ruleForm.areaOutAlarm = false
      } else {
        this.ruleForm.areaOutAlarm = true
        this.ruleForm.areaInAlarm = false
      }
    },
    onPointRuleTypeChange(e) {
      console.log('onPointRuleTypeChange', e)
      if (e) {
        this.ruleForm.pointUnarrive = true
        this.ruleForm.pointUnleave = false
      } else {
        this.ruleForm.pointUnleave = true
        this.ruleForm.pointUnarrive = false
      }
    },
    onOfflineRuleTypeChange(e) {
      console.log('onOfflineRuleTypeChange', e)
      if (e) {
        this.ruleForm.offlineUnnormal = true
        this.ruleForm.offlineShift = false
      } else {
        this.ruleForm.offlineShift = true
        this.ruleForm.offlineUnnormal = false
      }
    },
    onAreaTypeChange(event) {
      console.log('onAreaTypeChange', event)
      if ((this.ruleType == 1 || this.ruleType == 3) && event == 1) {
        // 自定义区域
        this.requestCustomArea()
      } else if (this.ruleType == 1 && event == 2) {
        // 线路
        this.requestCustomLine()
      }
    }
  },
  mounted() {
    this.init()
  }
}
</script>
